package yxy.game.pm2.module.pickedtroops;


import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.commandertally;
import yxy.cherry.data.bean.commandertallylevel;
import yxy.game.pm2.bean.PickedTroopsSlot;
import yxy.game.pm2.message.PickedTroopsMessage;

import java.util.ArrayList;

public class PickedTroopsHandle {

    /**
     * 获取背包的兵符或某阵容的兵符
     *
     * @param slot
     * @param camp
     * @return
     */
    public static PickedTroopsMessage.PickedTroopsSlot packPickedTroopsSolt(PickedTroopsSlot slot, int camp) {
        //如果输入了阵营  兵符不是那个阵营就不返回
        if (camp != 0 && slot.getType() != camp) {
            return null;
        }
        return getSlot(slot);
    }


    /**
     * 属性内容封装
     *
     * @param pts
     * @param pickedTroopsSolt
     * @return
     */
    public static PickedTroopsMessage.Property getProperty(PickedTroops pts, PickedTroopsSlot pickedTroopsSolt) {
        //计算兵符槽和兵符总共的属性加成
        PickedTroopsMessage.Property.Builder property = PickedTroopsMessage.Property.newBuilder();

        //兵符槽
        commandertallylevel data = DataCenter.getData(pts.getCamp() + "_" + pts.getLevel(), commandertallylevel.class);
        int attack = data.getAttack();
        int defense = data.getDefense();
        int life = data.getLife();

        //兵符
        commandertally commandertally = DataCenter.getData(pickedTroopsSolt.getDataId(), commandertally.class);
        attack += commandertally.getAttack();
        defense += commandertally.getDefense();
        life += commandertally.getLife();

        property.setAttack(attack);
        property.setDefense(defense);
        property.setLife(life);
        return property.build();
    }


    /**
     * 兵符内容封装
     *
     * @param pickedTroopsSolt
     * @return
     */
    public static PickedTroopsMessage.PickedTroopsSlot getSlot(PickedTroopsSlot pickedTroopsSolt) {
        PickedTroopsMessage.PickedTroopsSlot.Builder slot = PickedTroopsMessage.PickedTroopsSlot.newBuilder();
        String code = pickedTroopsSolt.getCode();
        int dataId = pickedTroopsSolt.getDataId();
        ArrayList<Integer> entryId = pickedTroopsSolt.getEntryId();//词条id
        ArrayList<Integer> value = pickedTroopsSolt.getValue();//词条的值
        ArrayList<Integer> isLock = pickedTroopsSolt.getIsLock();//锁的情况
        ArrayList<Integer> reEntryId = pickedTroopsSolt.getReEntryId();//洗练后的词条
        ArrayList<Integer> reValue = pickedTroopsSolt.getReValue();//洗练后词条的值
        //添加兵符的词条
        for (int i = 0; i < entryId.size(); i++) {
            PickedTroopsMessage.Entry.Builder entry = PickedTroopsMessage.Entry.newBuilder();
            entry.setId(entryId.get(i));
            entry.setValue(value.get(i));
            entry.setIsLock(isLock.get(i));
            slot.addEntrys(entry);
        }
        slot.setCode(code);//兵符的code
        slot.setId(dataId);//兵符的id
        //添加洗练的词条
        for (int i = 0; reEntryId != null && i < reEntryId.size(); i++) {
            PickedTroopsMessage.Entry.Builder entry = PickedTroopsMessage.Entry.newBuilder();
            entry.setId(reEntryId.get(i));
            entry.setValue(reValue.get(i));
            slot.addReEntrys(entry);
        }
        //属性
        commandertally commandertally = DataCenter.getData(pickedTroopsSolt.getDataId(), commandertally.class);
        slot.setAttack(commandertally.getAttack());
        slot.setDefense(commandertally.getDefense());
        slot.setLife(commandertally.getLife());
        return slot.build();
    }


    /**
     * 某阵容的兵符槽信息
     *
     * @param playerCode
     * @param pts
     * @return
     */
    public static PickedTroopsMessage.PickedTroops getPickedTroops(String playerCode, PickedTroops pts) {
        PickedTroopsMessage.PickedTroops.Builder build = PickedTroopsMessage.PickedTroops.newBuilder();
        build.setCamp(pts.getCamp());
        build.setUnlock(pts.isOpen());
        if (pts.isOpen()) {
            String slotCode = pts.getPickedTroopsSolt();
            PickedTroopsSlot pickedTroopsSolt = PickedTroopsSlot.get(playerCode, slotCode);
            PickedTroopsMessage.Property property = getProperty(pts, pickedTroopsSolt);//属性的内容
            PickedTroopsMessage.PickedTroopsSlot slot = getSlot(pickedTroopsSolt);//兵符的内容
            //返回
            build.setLevel(pts.getLevel());
            build.setStage(pts.getStagelevel());
            build.setSlot(slot);
            build.setProperty(property);
            build.setDamageincrease(pts.getDamageincrease());
            build.setDamagedrop(pts.getDamagedrop());
            //升级或升阶的状态  0：已经到极限不能再升级/升阶    1：升级     2：升阶
            commandertallylevel commandertallylevel = DataCenter.getData(pts.getCamp() + "_" + (pts.getLevel() + 1), commandertallylevel.class);
            if (commandertallylevel == null) build.setIsLevelOrStageUp(0);//已满级
            if (commandertallylevel != null && commandertallylevel.getStar() == pts.getStagelevel())
                build.setIsLevelOrStageUp(1);//升级
            if (commandertallylevel != null && commandertallylevel.getStar() != pts.getStagelevel())
                build.setIsLevelOrStageUp(2);//升阶
        }
        return build.build();
    }
}
